Repository navigation
Icons: Add getAspectRatioIcon and use it in the media editor - #86348
Conversation
… grid Move the icon that draws the shape of an aspect ratio into @wordpress/components as a private component, and round the sides of the shapes to a half pixel so their outlines sit on the same grid as the generic aspect ratio icon.
🤖 PR meta 🤖🎉 PropsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the 🏷️ LabelsThis pull request needs exactly one label indicating its type, and has 0.
Read more about Type labels in Gutenberg. If you cannot add labels yourself, a reviewer can do it for you. |
…icon-shared # Conflicts: # packages/block-editor/CHANGELOG.md # packages/block-editor/src/components/dimensions-tool/aspect-ratio-icon.tsx # packages/components/CHANGELOG.md
mirka
left a comment
There was a problem hiding this comment.
I'm actually wondering if @wordpress/icons could be the right home for this… Like a function that returns an SVG element: <Icon icon={ getAspectRatioIcon( 16 / 9 ) } />.
It would be the first non-static export in that package, so it would live next to Icon rather than in library/, and storybook/stories/icons/manifest.ts would need to skip it. What do you think @t-hamano?
|
@00kaku Thanks for working on this!
Sorry, I missed this. It means that whether we put this dynamic icon in My main question is whether @ramonjd @andrewserong, are there any plans to expose the media editor on |
@ramonjd can correct me here, but in the short-term I'd say it's unlikely to be exposed on the wp global until we look at stabilising the media editor route experiment, which is to say if/when we replace the core media editor with the new one. That's likely a long way off as we still have a bunch of unknowns (particular in regards to extensibility), but we're chipping away at building out features, so we are making progress 🙂 For the purposes here, I'd assume that (If it's a problem, personally I don't mind too much if the media editor's icon isn't up to date for a while) |
|
@andrewserong Thanks for the reply!
Bundled packages are not allowed to import private APIs. So I think we have two options:
I don't have any concerns about making this dynamic icon public. I just wanted to keep it private if possible 🙂 |
I usually lean that way for new components, too, as once we make something public we're committed to it. In this case, though, the API surface is very small and seems unlikely to change anytime soon (a simple component with a
That is also perfectly valid, though. I wouldn't consider this urgent for the media editor itself, and if y'all prefer to keep this private for now, we can add this as a to-do list item for the media editor so we remember to tidy this one when it's eventually stable. |
|
I think we can go with |
Move the aspect ratio icon from a private component in @wordpress/components to a public function in @wordpress/icons, so the bundled media editor can use it. The block editor and the media editor's crop panel use it, and the icon sets fill: none inline so parent fill rules do not fill the shape.
|
Thanks all, this is much clearer now! I've gone with I also applied the review fixes: |
| { | ||
| "title": "AspectRatioIcon", | ||
| "slug": "aspect-ratio-icon", | ||
| "markdown_source": "../packages/components/src/aspect-ratio-icon/README.md", | ||
| "parent": "components" | ||
| }, |
There was a problem hiding this comment.
This is no longer needed. It should be removed once you run npm run docs:build.
There was a problem hiding this comment.
Removed by running npm run docs:build, thanks! That should also fix the failing "Check local changes" CI step.
| it( 'draws an icon for each aspect ratio option', async () => { | ||
| const user = userEvent.setup(); | ||
| setupCropPanel(); | ||
|
|
||
| await user.click( | ||
| screen.getByRole( 'combobox', { name: 'Aspect ratio' } ) | ||
| ); | ||
|
|
||
| const square = await screen.findByRole( 'option', { name: 'Square' } ); | ||
|
|
||
| // The icon has no accessible name, so look for the SVG itself. | ||
| // eslint-disable-next-line testing-library/no-node-access | ||
| expect( square.querySelector( 'svg' ) ).toBeInTheDocument(); | ||
| } ); |
There was a problem hiding this comment.
Select.Item always renders a check icon, and for unselected items it's simply hidden with opacity: 0. So this test would pass even without the aspect ratio icon.
I think we can just remove this test. The shape and the fallback to the generic icon are already covered by the unit tests in @wordpress/icons.
There was a problem hiding this comment.
Good catch, I've removed the test. The shape and the fallback to the generic icon are covered by the unit tests in @wordpress/icons.
| .media-editor-crop-panel__aspect-ratio-option { | ||
| display: flex; | ||
| align-items: center; | ||
| gap: $grid-unit-10; | ||
|
|
||
| svg { | ||
| flex-shrink: 0; | ||
| } | ||
| } |
There was a problem hiding this comment.
When the media editor is opened as a modal from the editor, it receives the aspect ratios defined in theme.json, so long labels can be passed in. So it might be better to add styles that account for truncation, as in the block editor's DimensionsTool implementation.
There was a problem hiding this comment.
Added the same truncation styles as the DimensionsTool. I checked in the media editor that a long label ends with an ellipsis in the trigger, and the icon keeps its size.
mirka
left a comment
There was a problem hiding this comment.
On narrow screens, aspect ratio is picked from the Menu in MediaEditorImageControls, and that menu still shows text only. Would it be worth adding the icons there too? (I like how our icon abstraction turned out to be handy for things like this too, instead of abstracting the full Select 😄)
| Number( item.value ) | ||
| ) } | ||
| /> | ||
| { item.label } |
There was a problem hiding this comment.
The trigger's ellipsis doesn't apply through the flex wrapper, so a long label is cut off without one. On trunk, the default trigger truncated it. The block editor's version handles this with a label span plus min-width: 0 on the wrapper. Can we do the same here, along with the matching __aspect-ratio-label rule in style.scss?
| { item.label } | |
| <span className="media-editor-crop-panel__aspect-ratio-label"> | |
| { item.label } | |
| </span> |
There was a problem hiding this comment.
Done, the trigger label is now a span with the __aspect-ratio-label class, min-width: 0 is on the wrapper, and the matching rule is in style.scss, as in the block editor.
…narrow-screen menu Wrap the trigger label in a span with min-width: 0 on the wrapper so a long label gets an ellipsis, show the aspect ratio icons in the menu used on narrow screens, remove a stale AspectRatioIcon entry from docs/manifest.json and drop a test that passed without the icon.
@mirka Good idea, thanks! The narrow-screen menu now shows the aspect ratio icons too, using
And agreed, the icon function was handy here. |


What?
Follow-up to #85812 and #86344
Adds
getAspectRatioIcon( ratio )to@wordpress/icons: a function that returns an icon drawing the shape of an aspect ratio. The block editor's aspect ratio dropdown uses it instead of its local copy, and the media editor's crop panel dropdown now shows the same icons.Why?
The aspect ratio icons from #85812 only existed inside the block editor. The media editor's dropdown, suggested by @t-hamano, needs them too, but
@wordpress/media-editoris a bundled package and can't use private APIs. After the discussion in this PR, @mirka suggested@wordpress/icons, which is itself bundled, so the API stays safe to change or remove.How?
@wordpress/icons: addsgetAspectRatioIcon( ratio ), exported next toIconand used as<Icon icon={ getAspectRatioIcon( 16 / 9 ) } />. The ratio is a number (width divided by height). A value that is not a positive number, such as0, gets the genericaspectRatioicon.fill: noneinline, as the other stroke icons do, so a parent rule like.components-button svg { fill: currentColor }does not fill the rectangle.@wordpress/block-editor: the dimensions tool usesgetAspectRatioIconand its local copy is removed.@wordpress/media-editor: the crop panel's aspect ratio dropdown shows the icons in the trigger and in the options. Free and Original use the generic icon, because drawing the image's own ratio would need its dimensions.Testing Instructions
Automated:
npm run test:unit -- packages/icons packages/media-editor/src/components/media-editor-crop-panel packages/block-editor/src/components/dimensions-toolUse of AI Tools
I used Claude Code (Anthropic) to help investigate the issue, locate the cause in the code, and draft the fix. I reviewed the changes, ran the tests and verified the behavior in the browser myself, and I take responsibility for them.